package com.baidu.controller;

import com.baidu.utils.Pages;
import com.baidu.pojo.User;
import com.baidu.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/AdminUser")
public class AdministratorController {
    @Autowired
    private AdministratorService administratorService;

    //用户登录
    @RequestMapping("/login")
    public String login(User user, HttpSession session){
        System.out.println("------------------login-----------------");
        System.out.println("user:"+user);

        //调用service,实现登录
        User user2=administratorService.login(user);

        if(user2!=null){//登录成功
            session.setAttribute("user2",user2);
            return "main";
        }else {//登录失败
//            return "forward:/login.jsp";
            return "redirect:/login.jsp";
        }
    }

    //分页查询所有用户信息
    @RequestMapping("/getAllUser")
    public String getByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                            @RequestParam(defaultValue = "3") Integer pageSize,
                            HttpServletRequest request,
                            HttpSession session){
        //创建pages对象
            Pages pages=new Pages();
            //设置当前页
            pages.setCurrentPage(currentPage);
            //设置页面容量
            pages.setPageSize(pageSize);
            //查询总记录数
            int total=administratorService.count();
            //设置总记录数
            pages.setTotalCount(total);
            //将pages对象保存到request中
            request.setAttribute("pages",pages);
            //调用service，获取查询结果
            List<User> list = administratorService.getByPage(currentPage,pageSize);
            //将集合保存到request中
            request.setAttribute("list",list);
        return "AdminUserList";
    }

    //添加用户
    @RequestMapping("/addUser")
    public String addUser(User user){
        System.out.println("-----------------addUser---------------");
        System.out.println("user:"+user);
        Date now = new Date();
        user.setUtime(now);
        //执行添加
        administratorService.addUser(user);

        //重定向到/user/getAllUser
        return "redirect:/AdminUser/getAllUser";
    }

    //对用户名进行验证
    @RequestMapping("/checkName")
    @ResponseBody
    public boolean checkName(String uname){
        System.out.println("-------------checkName----------");
        System.out.println("uname:"+uname);
        return administratorService.myCheck(uname);
    }

    //根据id删除指定用户
    @RequestMapping("/deletById")
    public String deletById(Integer uid){
        System.out.println("-------------deletById-------------");
        System.out.println("user:"+uid);

        //执行删除
        administratorService.deleteUserById(uid);

        //重定向请求到/user/getAllUser
        return "redirect:/AdminUser/getAllUser";
    }
    //根据id查询指定的用户信息
    @RequestMapping("/getUserById")
    public String getUserById(Integer uid,HttpServletRequest request){
        System.out.println("------------getUserById-------");
        System.out.println("id:"+uid);

        //根据id查询指定的用户信息
        User user=administratorService.getUserById(uid);

        //将对象保存到request中
        request.setAttribute("user",user);
        return "AdminUserDetail";
    }


    //进入编辑页面
    @RequestMapping("/toEditUser")
    public String toEditUser(Integer uid,HttpServletRequest request){
        System.out.println("---------------toEditUser-------------------");
        System.out.println("id:"+uid);

        //根据id查询指定的用户信息
        User user=administratorService.getUserById(uid);

        //将对象保存到request中
        request.setAttribute("user",user);

        return "AdminUserEdit";
    }


    //根据id修改指定的用户信息
    @RequestMapping("/updateUser")
    public String updateUser(User user){
        System.out.println("------------updateUser--------------");
        System.out.println("user:"+user);
        Date now = new Date();
        user.setUtime(now);
        //执行修改
        administratorService.updateUserById(user);

        return "redirect:/AdminUser/getAllUser";
    }

}
